<template>
  <div class="contaiter">
    <el-form
      ref="courses"
      :model="courses"
      label-position="left"
      label-width="150px"
      :rules="rules"
    >
      <el-form-item label="课程名称" required prop="coursesName">
        <span slot="label" class="left-label">
          <span calss="cn-label">课程名称</span><br>
          <span class="en-label">Course Name</span>
        </span>
        <el-input
          v-model="courses.coursesName"
          placeholder="请输入课程名称"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="完成时间" required prop="time">
        <span slot="label" class="left-label">
          <span calss="cn-label">完成时间</span><br>
          <span class="en-label">Completion time</span>
        </span>
        <el-date-picker
          v-model="courses.time"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择完成时间"
        />
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="cancel">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm('courses')"
        >确定</el-button>
        <el-popconfirm
          v-if="showDel"
          title="确定删除吗？"
          class="delete-icon"
          @confirm="deleteConfirm"
        >
          <i slot="reference" class="delete-icon" />
        </el-popconfirm>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      courses: {
        coursesName: '', // 证书名称
        time: '' // 获得时间
      },
      rules: {
        coursesName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '请选择完成时间', trigger: 'change' }
        ]
      },
      index: null,
      showDel: false
    }
  },
  created() {},
  mounted() {},
  methods: {
    businessAreaChange(e) {
      console.log(e)
    },
    handleCityChange(e) {
      console.log(e)
    },
    cancel() {
      this.courses = {
        coursesName: '', // 证书名称
        time: '' // 获得时间
      }
      this.showDel = false
      this.index = null
      this.$parent.$parent.closeFun('courses')
      this.$nextTick(() => {
        this.$refs['courses'].clearValidate()
      })
    },
    deleteConfirm() {
      this.$parent.$parent.deleteFun('courses', this.index)
      this.cancel()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.courses))
          this.$parent.$parent.upDate('courses', data, this.index)
          this.cancel()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    init(data, index) {
      data = JSON.parse(JSON.stringify(data))
      this.courses = data
      this.index = index
      this.showDel = true
    }
  }
}
</script>

<style lang="scss" scoped>
.delete-icon {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  display: inline-block;
  width: 32px;
  height: 32px;
  background: url("../../../../assets/images/delete_icon.png") no-repeat center
    center / cover;
}
</style>
